package com.wenyu.wenyuyuedu.activity.personage;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.wenyu.wenyuyuedu.R;
import com.wenyu.wenyuyuedu.activity.LoginActivity;
import com.wenyu.wenyuyuedu.bean.personage.FeedbookBean;
import com.wenyu.wenyuyuedu.global.ApiWrapper;
import com.wenyu.wenyuyuedu.server.personage.FeedBookServer;
import com.wenyu.wenyuyuedu.utils.AbSharedUtil;
import com.wenyu.wenyuyuedu.utils.MyToast;
import com.wenyu.wenyuyuedu.utils.SharedConstants;
import com.wenyu.wenyuyuedu.utils.StatusBarUtil;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static anetwork.channel.http.NetworkSdkSetting.context;

/*意见与反馈*/
public class IdeaAndFeedBackActivity extends AppCompatActivity implements View.OnClickListener {

    @InjectView(R.id.iv_public_left)
    ImageView ivPublicLeft;
    @InjectView(R.id.tv_public_title)
    TextView tvPublicTitle;
    @InjectView(R.id.my_public_content)
    TextView myPublicContent;
    @InjectView(R.id.et_public_content)
    EditText etPublicContent;
    @InjectView(R.id.tv_public_sum)
    TextView tvPublicSum;
    @InjectView(R.id.tv_mycircle_residue)
    TextView tvMycircleResidue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_idea_and_feed_back);
        StatusBarUtil.setStatusBar(IdeaAndFeedBackActivity.this, R.color.white);
        ButterKnife.inject(this);
        initView();
    }

    /*初始化控件*/
    private void initView() {
        ivPublicLeft.setOnClickListener(this);
        myPublicContent.setOnClickListener(this);
        etPublicContent.addTextChangedListener(new IdeaAndFeedBackActivity.MyEditTextChangeListener());
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            /*返回*/
            case R.id.iv_public_left:
                finish();
                break;
            /*提交*/
            case R.id.my_public_content:
                SubmitData();
                break;

            default:

                break;
        }
    }

    /*提交*/
    private void SubmitData() {
        String token = AbSharedUtil.getString(IdeaAndFeedBackActivity.this, SharedConstants.USER_TOKEN);
        String content = etPublicContent.getText().toString().trim();
        if (content.equals("")) {
            MyToast.showToast(IdeaAndFeedBackActivity.this, "请输入内容", Toast.LENGTH_SHORT);
        } else {
            FeedBookServer feedBookServer = ApiWrapper.getInstance().create(FeedBookServer.class);
            Call<FeedbookBean> feedBook = feedBookServer.getFeedBook(token, content);
            feedBook.enqueue(new Callback<FeedbookBean>() {
                @Override
                public void onResponse(Call<FeedbookBean> call, Response<FeedbookBean> response) {
                    FeedbookBean body = response.body();
                    if (body != null) {
                        int code = body.getCode();
                        if (code == 1) {
                            MyToast.showToast(IdeaAndFeedBackActivity.this, "提交成功", Toast.LENGTH_SHORT);
                            etPublicContent.setText("");
                        } else if (code == 0) {
                            showLoginDoalog();
                        }
                    }
                }

                @Override
                public void onFailure(Call<FeedbookBean> call, Throwable t) {

                }
            });
        }

    }

    /*编辑的监听*/
    class MyEditTextChangeListener implements TextWatcher {
        private CharSequence temp;

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            temp = charSequence;
        }

        @Override
        public void afterTextChanged(Editable editable) {
            tvMycircleResidue.setText(temp.length() + "");
        }
    }

    android.app.AlertDialog ad;
    private TextView quxiao;
    private TextView queding;

    /*显示登陆的dialog*/
    private void showLoginDoalog() {

        ad = new android.app.AlertDialog.Builder(context).create();
        ad.show();
        Window window = ad.getWindow();
        window.setContentView(R.layout.dailog_login);
        quxiao = (TextView) window.findViewById(R.id.dialog_login_quxiao);
        queding = (TextView) window.findViewById(R.id.dialog_login_queding);
        quxiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ad.dismiss();
            }
        });
        queding.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(IdeaAndFeedBackActivity.this, LoginActivity.class);
                startActivity(intent);
                finish();
                ad.dismiss();
            }
        });
    }
}
